{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-18T06:46:52.098105Z",
     "start_time": "2025-08-18T06:46:51.997436Z"
    }
   },
   "source": "import numpy as np",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 多维性",
   "id": "fa13d5b498427df2"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T06:47:00.495944Z",
     "start_time": "2025-08-18T06:47:00.491399Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array(5)  # 创建0维的ndarray数组\n",
    "print(arr)\n",
    "print('arr的维度：', arr.ndim)   # 数组的维度number of dimensions"
   ],
   "id": "dccc7d003a4acff4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "arr的维度： 0\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T06:47:25.798030Z",
     "start_time": "2025-08-18T06:47:25.792317Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1, 2, 3])  # 创建1维的ndarray数组\n",
    "print(arr)\n",
    "print('arr的维度：', arr.ndim)   # 数组的维度number of dimensions"
   ],
   "id": "1e32039b083d577c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "arr的维度： 1\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T06:48:00.833745Z",
     "start_time": "2025-08-18T06:48:00.830202Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([ [1, 2, 3], [4, 5, 6]])  # 创建2维的ndarray数组\n",
    "print(arr)\n",
    "print('arr的维度：', arr.ndim)   # 数组的维度number of dimensions"
   ],
   "id": "4c6e0009eb55380c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "arr的维度： 2\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 同质性",
   "id": "3a9ea39826531eaf"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T06:59:59.722737Z",
     "start_time": "2025-08-18T06:59:59.717655Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1, 'hello'])  # 不同的数据类型会被强制转换成相同的数据类型\n",
    "print(arr)"
   ],
   "id": "ee93404d1e98986c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1' 'hello']\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:00:17.549823Z",
     "start_time": "2025-08-18T07:00:17.546308Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1, 2.5])  # 不同的数据类型会被强制转换成相同的数据类型\n",
    "print(arr)"
   ],
   "id": "2d3130aab8f329e4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.  2.5]\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### ndarray的属性",
   "id": "38ee489d1c5d22bf"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:01:10.208964Z",
     "start_time": "2025-08-18T07:01:10.203762Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array(1)\n",
    "print(arr)\n",
    "print('数组的形状：', arr.shape)\n",
    "print('数组的维度：', arr.ndim)\n",
    "print('元素的个数：', arr.size)\n",
    "print('元素的数据类型：', arr.dtype)\n",
    "print('元素的转置', arr.T)"
   ],
   "id": "58a70dfa37812082",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "数组的形状： ()\n",
      "数组的维度： 0\n",
      "元素的个数： 1\n",
      "元素的数据类型： int64\n",
      "元素的转置 1\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:01:22.401551Z",
     "start_time": "2025-08-18T07:01:22.397401Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1, 2.5, 3])\n",
    "print(arr)\n",
    "print('数组的形状：', arr.shape)\n",
    "print('数组的维度：', arr.ndim)\n",
    "print('元素的个数：', arr.size)\n",
    "print('元素的数据类型：', arr.dtype)\n",
    "print('元素的转置', arr.T)"
   ],
   "id": "1aa0e88df69eeacf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.  2.5 3. ]\n",
      "数组的形状： (3,)\n",
      "数组的维度： 1\n",
      "元素的个数： 3\n",
      "元素的数据类型： float64\n",
      "元素的转置 [1.  2.5 3. ]\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:01:34.760495Z",
     "start_time": "2025-08-18T07:01:34.756494Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([[1,2,3],[4,5,6]])\n",
    "print(arr)\n",
    "print('数组的形状：', arr.shape)\n",
    "print('数组的维度：', arr.ndim)\n",
    "print('元素的个数：', arr.size)\n",
    "print('元素的转置', arr.T)"
   ],
   "id": "b2f8cc5469968ae8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "数组的形状： (2, 3)\n",
      "数组的维度： 2\n",
      "元素的个数： 6\n",
      "元素的转置 [[1 4]\n",
      " [2 5]\n",
      " [3 6]]\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### ndarray的创建",
   "id": "2bc48065c61f2138"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:03:02.990401Z",
     "start_time": "2025-08-18T07:03:02.984424Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 基础的创建方法\n",
    "list1 = [4, 5, 6]\n",
    "arr = np.array(list1,dtype=np.float64)\n",
    "print(arr.ndim)  # 属性\n",
    "print(arr)"
   ],
   "id": "e69edc39de07631f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "[4. 5. 6.]\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:03:34.620386Z",
     "start_time": "2025-08-18T07:03:34.615408Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# copy\n",
    "arr1 = np.copy(arr)  # 元素跟原始的数组相同，但是不是一个数组了\n",
    "print(arr1)\n",
    "arr1[0] = 8\n",
    "print(arr1)\n",
    "print(arr)"
   ],
   "id": "bbdcc2d9cc074bda",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4. 5. 6.]\n",
      "[8. 5. 6.]\n",
      "[4. 5. 6.]\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:03:52.516004Z",
     "start_time": "2025-08-18T07:03:52.512134Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 预定义形状\n",
    "# 全0  全1  未初始化  固定值\n",
    "# 全0\n",
    "arr = np.zeros((2,3),dtype=int)\n",
    "print(arr)\n",
    "print(arr.dtype)"
   ],
   "id": "e434c758296dbb88",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0]\n",
      " [0 0 0]]\n",
      "int64\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:04:13.852505Z",
     "start_time": "2025-08-18T07:04:13.849268Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.zeros((200,),dtype=int)\n",
    "print(arr)"
   ],
   "id": "14f48b2f99c3e632",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
      " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
      " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
      " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
      " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
      " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:04:32.481334Z",
     "start_time": "2025-08-18T07:04:32.477214Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 全1\n",
    "arr = np.ones((5,8),dtype=int)\n",
    "print(arr)"
   ],
   "id": "65d664b359d47755",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1]\n",
      " [1 1 1 1 1 1 1 1]]\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:04:55.270426Z",
     "start_time": "2025-08-18T07:04:55.267006Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 未初始化\n",
    "arr = np.empty((2,3))\n",
    "print(arr)"
   ],
   "id": "4d32adc21abcf2b6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0.]\n",
      " [0. 0. 0.]]\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:05:11.067016Z",
     "start_time": "2025-08-18T07:05:11.063781Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.empty((4, 2))\n",
    "print(arr)"
   ],
   "id": "8ba3813bbdf0e054",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0.]\n",
      " [0. 0.]\n",
      " [0. 0.]\n",
      " [0. 0.]]\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:05:24.834580Z",
     "start_time": "2025-08-18T07:05:24.831189Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.full((3,4),2025)\n",
    "print(arr)"
   ],
   "id": "cb53ddb210550c1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2025 2025 2025 2025]\n",
      " [2025 2025 2025 2025]\n",
      " [2025 2025 2025 2025]]\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:05:38.300289Z",
     "start_time": "2025-08-18T07:05:38.296164Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr1 = np.zeros_like(arr)\n",
    "print(arr1)\n",
    "arr1 = np.empty_like(arr)\n",
    "print(arr1)\n",
    "arr1 = np.ones_like(arr)\n",
    "print(arr1)\n",
    "arr1 = np.full_like(arr,2026)\n",
    "print(arr1)"
   ],
   "id": "ad30041315d5715b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0 0]\n",
      " [0 0 0 0]\n",
      " [0 0 0 0]]\n",
      "[[      2054841803992                  50                   0\n",
      "                    0]\n",
      " [  31525691312832512 7146768841584043371 3270519753163486258\n",
      "  3558464024330462774]\n",
      " [7002307083063799141 3846418444621723961 8386112019185689701\n",
      "  4122263037742904181]]\n",
      "[[1 1 1 1]\n",
      " [1 1 1 1]\n",
      " [1 1 1 1]]\n",
      "[[2026 2026 2026 2026]\n",
      " [2026 2026 2026 2026]\n",
      " [2026 2026 2026 2026]]\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:06:00.887381Z",
     "start_time": "2025-08-18T07:06:00.883979Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 等差数列 2 4 6 8 10\n",
    "arr = np.arange(1, 51, 1) # start,end,step(步长）\n",
    "print(arr)"
   ],
   "id": "e54abcadc34294be",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24\n",
      " 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48\n",
      " 49 50]\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:06:15.699651Z",
     "start_time": "2025-08-18T07:06:15.695524Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 等间隔数列\n",
    "arr = np.linspace(0,100,5)\n",
    "print(arr)"
   ],
   "id": "59aec1213a16f2ec",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  0.  25.  50.  75. 100.]\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:06:26.605607Z",
     "start_time": "2025-08-18T07:06:26.601236Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.linspace(0,100,5,dtype=int)\n",
    "print(arr)\n",
    "arr = np.arange(0,101, 25)\n",
    "print(arr)"
   ],
   "id": "75a9ef7b6ad0339a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  0  25  50  75 100]\n",
      "[  0  25  50  75 100]\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:06:38.140669Z",
     "start_time": "2025-08-18T07:06:38.137176Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 对数间隔数列\n",
    "arr = np.logspace(0,4,3,base=2)\n",
    "print(arr)"
   ],
   "id": "94f4a12d626fe667",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.  4. 16.]\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:07:14.980685Z",
     "start_time": "2025-08-18T07:07:14.976678Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 特殊矩阵\n",
    "# 单位矩阵：主对角线上的数字为1，其他的数字为0\n",
    "arr = np.eye(3,4,dtype=int)\n",
    "print(arr)"
   ],
   "id": "b0b15b5cdba07cb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 0 0 0]\n",
      " [0 1 0 0]\n",
      " [0 0 1 0]]\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:07:35.007204Z",
     "start_time": "2025-08-18T07:07:35.003877Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 对角矩阵：主对角线上非零的0，其他的数字为0\n",
    "arr = np.diag([5,1,2,3])\n",
    "print(arr)"
   ],
   "id": "a90482a5cda741af",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 0 0 0]\n",
      " [0 1 0 0]\n",
      " [0 0 2 0]\n",
      " [0 0 0 3]]\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:08:11.853383Z",
     "start_time": "2025-08-18T07:08:11.835225Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 随机数组的生成\n",
    "# 生成0到1之间的随机浮点数（均匀分布）\n",
    "arr = np.random.rand(2,3)\n",
    "print(arr)"
   ],
   "id": "bd417d3e0d9972f0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.79963367 0.34896736 0.75790297]\n",
      " [0.41821228 0.72054607 0.3130874 ]]\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:08:25.004363Z",
     "start_time": "2025-08-18T07:08:25.000612Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 生成指定范围区间的随机浮点数\n",
    "arr = np.random.uniform(3, 6,(2,3))\n",
    "print(arr)"
   ],
   "id": "7bdf974d8c9f380e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3.93683142 4.156177   5.56355237]\n",
      " [5.48723312 3.97090517 5.32678959]]\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:08:38.195530Z",
     "start_time": "2025-08-18T07:08:38.192185Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 生成指定范围区间的随机整数\n",
    "arr = np.random.randint(3, 30,(2,3))\n",
    "print(arr)"
   ],
   "id": "be764190bd7a963e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5 21 18]\n",
      " [10 18 17]]\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:08:50.906868Z",
     "start_time": "2025-08-18T07:08:50.903833Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 生成随机数列（正态分布）（-3到3之间）\n",
    "# 两边小，中间大\n",
    "arr = np.random.randn(2,3)\n",
    "print(arr)"
   ],
   "id": "c9f2af1d746f58e1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.32467985 -1.15134819 -0.17789442]\n",
      " [-0.40916986 -1.63141457  0.07373328]]\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:09:10.692457Z",
     "start_time": "2025-08-18T07:09:10.689132Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 设置随机种子\n",
    "np.random.seed(20)\n",
    "arr = np.random.randint(1,10,(2,5))\n",
    "print(arr)"
   ],
   "id": "b86b45aa767e3b3b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4 5 7 8 3]\n",
      " [1 7 9 6 4]]\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:09:31.352615Z",
     "start_time": "2025-08-18T07:09:31.349126Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1,2,3],dtype='i8')\n",
    "print(arr)"
   ],
   "id": "42176b84406a4c47",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### ndarray的数据类型",
   "id": "486dc096a24d9fd2"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:10:53.313804Z",
     "start_time": "2025-08-18T07:10:53.309310Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([1,0,127,0],dtype= np.int8)\n",
    "print(arr)"
   ],
   "id": "de54b2eca18b9c81",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  1   0 127   0]\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 索引与切片",
   "id": "8b0fa8c87be6e0c4"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:11:40.462077Z",
     "start_time": "2025-08-18T07:11:40.456754Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 一维数组的索引与切片\n",
    "arr = np.random.randint(1,100,20)\n",
    "print(arr)"
   ],
   "id": "1075587f417d3371",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[17 63 17  8 99  7 27 14 76 59 26  4 75 76 62 78 84 58 95 33]\n"
     ]
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:11:56.690608Z",
     "start_time": "2025-08-18T07:11:56.686664Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(arr[10])\n",
    "print(arr[:]) # 获取全部的数据\n",
    "print(arr[2:5])  # start:end+1 左包右不包\n",
    "print(arr[slice(2,15,3)])  #start,end,step\n",
    "print(arr[ (arr>10)  & (arr<70) ]) # 布尔索引"
   ],
   "id": "fe1e1885ccedf726",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "26\n",
      "[17 63 17  8 99  7 27 14 76 59 26  4 75 76 62 78 84 58 95 33]\n",
      "[17  8 99]\n",
      "[17  7 76  4 62]\n",
      "[17 63 17 27 14 59 26 62 58 33]\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:12:14.595691Z",
     "start_time": "2025-08-18T07:12:14.592524Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 二维数组的索引与切片\n",
    "arr = np.random.randint(1,100,(4,8))\n",
    "print(arr)"
   ],
   "id": "9bc5ecfb6ef988af",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[11  7 76 19  4 78 18 44]\n",
      " [17 19 80 91 60 72 76 30]\n",
      " [46  7 64 79 25 19 94 34]\n",
      " [79 43 95  4 19 71 53 23]]\n"
     ]
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:12:28.316345Z",
     "start_time": "2025-08-18T07:12:28.312005Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(arr[1,3])  # 索引\n",
    "print(arr[ 1,2:5])\n",
    "print(arr[2][  arr[2] > 50 ])\n",
    "print( arr[:,3] )"
   ],
   "id": "f45f8063a92f4bba",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "91\n",
      "[80 91 60]\n",
      "[64 79 94]\n",
      "[19 91 79  4]\n"
     ]
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### numpy中常用的函数",
   "id": "5f23d6aef24d896c"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 基本的数学函数",
   "id": "20c7bb38f761ffc0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:13:36.278271Z",
     "start_time": "2025-08-18T07:13:36.272615Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算平方根\n",
    "print(np.sqrt(9))\n",
    "print(np.sqrt([1,4,9]))\n",
    "arr = np.array([1,25,81])\n",
    "print(np.sqrt(arr))"
   ],
   "id": "3cb632e494299a05",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.0\n",
      "[1. 2. 3.]\n",
      "[1. 5. 9.]\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:13:49.121934Z",
     "start_time": "2025-08-18T07:13:49.118848Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算指数  e^x  = y\n",
    "print(np.exp(1))"
   ],
   "id": "3db3821931319f46",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.718281828459045\n"
     ]
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:14:02.071135Z",
     "start_time": "2025-08-18T07:14:02.068033Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算自然对数  ln y = x\n",
    "print(np.log(2.71))"
   ],
   "id": "f2a7ea2edafd7a2b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9969486348916096\n"
     ]
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:14:13.621618Z",
     "start_time": "2025-08-18T07:14:13.618215Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算正弦值、余弦值\n",
    "print(np.sin(np.pi/2))\n",
    "print(np.cos(np.pi))"
   ],
   "id": "6ac834ed272e706c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n",
      "-1.0\n"
     ]
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:14:25.237757Z",
     "start_time": "2025-08-18T07:14:25.234636Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算绝对值\n",
    "arr = np.array([-1, 1, 2, -3])\n",
    "print(np.abs(arr))"
   ],
   "id": "754641b42965c9ae",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 1 2 3]\n"
     ]
    }
   ],
   "execution_count": 46
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:14:34.766440Z",
     "start_time": "2025-08-18T07:14:34.763599Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算a的b次幂\n",
    "print(np.power(arr,3))"
   ],
   "id": "5945104f5deac933",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ -1   1   8 -27]\n"
     ]
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:14:46.202854Z",
     "start_time": "2025-08-18T07:14:46.199029Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 四舍五入\n",
    "print(np.round([3.2, 4.5, 8.1, 9.6]))"
   ],
   "id": "7bae395eacf7b989",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3.  4.  8. 10.]\n"
     ]
    }
   ],
   "execution_count": 48
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:15:02.716662Z",
     "start_time": "2025-08-18T07:15:02.712743Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 向上取整，向下取整\n",
    "arr = np.array([1.6, 25.1, 81.7])\n",
    "print(np.ceil(arr))\n",
    "print(np.floor(arr))"
   ],
   "id": "157f91007ae1d6b6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 2. 26. 82.]\n",
      "[ 1. 25. 81.]\n"
     ]
    }
   ],
   "execution_count": 49
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:15:16.956710Z",
     "start_time": "2025-08-18T07:15:16.951413Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 检测缺失值NaN\n",
    "np.isnan([1, 2, np.nan, 3])"
   ],
   "id": "e6ec7752647968c4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False,  True, False])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 50
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 统计函数",
   "id": "95e979043fe3bd81"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:16:50.469636Z",
     "start_time": "2025-08-18T07:16:50.464701Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.random.randint(1,200,8)\n",
    "print(arr)"
   ],
   "id": "827945f2e8b77fd1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[143   5  28  76   9   7  27  83]\n"
     ]
    }
   ],
   "execution_count": 51
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:17:01.915622Z",
     "start_time": "2025-08-18T07:17:01.911873Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 求和\n",
    "print(np.sum([1,2,3]))"
   ],
   "id": "1335c04335df09cf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:17:14.347258Z",
     "start_time": "2025-08-18T07:17:14.343501Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算平均值\n",
    "print(np.mean(arr))"
   ],
   "id": "c18b0190e1c1d312",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "47.25\n"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:17:26.979014Z",
     "start_time": "2025-08-18T07:17:26.974982Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算中位数\n",
    "# 奇数：排序后中间的数值\n",
    "# 偶数：中间的两个数的平均值\n",
    "print(np.median([4,1,2]))\n",
    "print(np.median([1,1,1,1,1,1,1,1,1,100]))"
   ],
   "id": "450ac2d3e83fac12",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0\n",
      "1.0\n"
     ]
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:17:42.185207Z",
     "start_time": "2025-08-18T07:17:42.180609Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算标准差、方差\n",
    "# 1,2,3 的平均值 2\n",
    "# ((1-2)^2 + (2-2)^2 + (3-2)^2 )/ 3 = 0.666\n",
    "arr1 = np.array([1,2,1,2,1,1,1,2])\n",
    "arr2 = np.array([1,0,3,0,0,0,4,3])\n",
    "print(np.mean(arr1))\n",
    "print(np.mean(arr2))\n",
    "print(np.var(arr1))\n",
    "print(np.var(arr2))"
   ],
   "id": "d7f4c7fc798a8282",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.375\n",
      "1.375\n",
      "0.234375\n",
      "2.484375\n"
     ]
    }
   ],
   "execution_count": 55
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:17:56.292120Z",
     "start_time": "2025-08-18T07:17:56.288477Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 计算最大值、最小值\n",
    "print(arr)\n",
    "print(np.max(arr), np.argmax(arr))\n",
    "print(np.min(arr), np.argmin(arr))"
   ],
   "id": "1bb5373ed8fb7122",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[143   5  28  76   9   7  27  83]\n",
      "143 0\n",
      "5 1\n"
     ]
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:18:12.939356Z",
     "start_time": "2025-08-18T07:18:12.929773Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 分位数\n",
    "# 中位数\n",
    "# __________________\n",
    "# 1     2    3     4\n",
    "# 1       2        3\n",
    "print(np.median([1,2,3]))\n",
    "print(np.median([1,2,3,4]))\n",
    "np.random.seed(0)\n",
    "arr = np.random.randint(0,100,4)\n",
    "print(arr)\n",
    "# __________________\n",
    "# 44   47   64   67\n",
    "# (47+64)/2\n",
    "print(np.median(arr))\n",
    "print(np.percentile(arr,80))\n",
    "# 0.25*3=0.75\n",
    "# (47-44)*0.75=2.25 + 44 = 46.25\n",
    "# 3*0.8 = 2.4\n",
    "# (67-64)*0.4 = 1.2 + 64 = 65.2"
   ],
   "id": "d83ddbeb531425ce",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0\n",
      "2.5\n",
      "[44 47 64 67]\n",
      "55.5\n",
      "65.2\n"
     ]
    }
   ],
   "execution_count": 57
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:18:27.750574Z",
     "start_time": "2025-08-18T07:18:27.746063Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 累积和、累积积\n",
    "arr = np.array([1,2,3])\n",
    "print(np.sum(arr))\n",
    "print(np.cumsum(arr))\n",
    "print(np.cumprod(arr))"
   ],
   "id": "4ec5c3d49dbc8da5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "[1 3 6]\n",
      "[1 2 6]\n"
     ]
    }
   ],
   "execution_count": 58
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
